package vn.aptech.scrumgroup.model;

import vn.aptech.scrumgroup.util.DrawUtil;
import android.graphics.Canvas;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Rect;
import demo.axitdn.physic.Unit;

public class CircleWithTextModel extends AbstractBaseModel{

	protected float cx, cy, radius;
	protected String text;
	protected int idText;
	protected boolean drawOutline;
	protected float outlineWidth;
	protected float charWidth;
	protected float beginX;
	protected float beginY;
	
	public CircleWithTextModel(int backColor, int foreColor) {
		// TODO Auto-generated constructor stub
		super(backColor, foreColor);
	}
	public CircleWithTextModel(float cx, float cy, float radius, boolean drawOutline, float outlineWidth, String text, float charWidth, int backColor, int foreColor){
		super(backColor, foreColor);
		setProperties(cx, cy, radius, drawOutline, outlineWidth, text, charWidth);
	}//end setProperties
	
	
	public void setProperties(float cx, float cy, float radius, boolean drawOutline, float outlineWidth, String text, float charWidth ){
		this.cx = cx;
		this.cy = cy;
		this.radius = radius;
		this.drawOutline = drawOutline;
		this.outlineWidth = outlineWidth;
		this.text = text;
		this.charWidth = charWidth;
		this.charWidth = 1;
	}//end setProperties

	public float getCx() {
		return cx;
	}
	public void setCx(float cx) {
		this.cx = cx;
	}
	public float getCy() {
		return cy;
	}
	public void setCy(float cy) {
		this.cy = cy;
	}
	public float getRadius() {
		return radius;
	}
	public void setRadius(float radius) {
		this.radius = radius;
	}
	public String getText() {
		return text;
	}
	public void setText(String text) {
		this.text = text;
	}
	public boolean isDrawOutline() {
		return drawOutline;
	}
	public void setDrawOutline(boolean drawOutline) {
		this.drawOutline = drawOutline;
	}
	public float getOutlineWidth() {
		return outlineWidth;
	}
	public void setOutlineWidth(float outlineWidth) {
		this.outlineWidth = outlineWidth;
	}
	public float getCharWidth() {
		return charWidth;
	}
	public void setCharWidth(float charWidth) {
		this.charWidth = charWidth;
	}
	
	@Override
	public void onDraw(Canvas c) {
		// TODO Auto-generated method stub
		Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
		ColorMatrix cm = new ColorMatrix();
		int brightness = 120;
		if (isBrilliant){ 
			cm.set(new float[]{1,0,0,0,brightness,
		    		0,1,0,0,brightness,
		    		0,0,1,0,brightness,
		    		0,0,0,1,0
		    		});
			paint.setColorFilter(new ColorMatrixColorFilter(cm));
		}else {
			paint.setColorFilter(null);
		}//end else
		paint.setColor(backColor);
		drawCircle(c, cx, cy, radius, paint, drawOutline, outlineWidth);
		paint.setStyle(Style.FILL_AND_STROKE);
		paint.setStrokeWidth(charWidth);
		paint.setColor(foreColor);
		drawTextOnCircle(c, text, cx, cy, paint, charWidth);
	}//end onDraw
	
	private void drawCircle(Canvas canvas, float x, float y, float radius, Paint paint, boolean drawOutline, float outlineWidth)
	{
		if(drawOutline){
			paint.setStyle(Style.STROKE);
			paint.setStrokeWidth(outlineWidth);
		}
		canvas.drawCircle(x, y, radius, paint);
	}
	
	private void drawText(Canvas canvas, String text, float x, float y, Paint paint, float charWidth)
	{
		if(charWidth > 0){
			paint.setStrokeWidth(charWidth);
		}
		canvas.drawText(text, x, y , paint);
	}
	
	private void drawTextOnCircle(Canvas canvas, String text, float x, float y, Paint paint, float charWidth)
	{
		if(charWidth > 0){
			paint.setStrokeWidth(charWidth);
		}
		//split text to words
		String[] words = text.split(" ");
		//draw 1 word or 2 words
		if (words.length==1){
			Rect rect = new Rect();
			paint.setTextSize(DrawUtil.calculateTextSize(words, (radius)*Unit.i, paint, 3));
			paint.getTextBounds(text, 0, text.length(), rect);
			float text_X = x - rect.width() / 2;
			float text_Y = y + rect.height() / 2;
			drawText(canvas, text, text_X, text_Y, paint, charWidth);
		} else{
			int offset = 3;
			paint.setTextSize(DrawUtil.calculateTextSize(words, (radius)*Unit.i, paint, 3));
			Rect rect = new Rect();
			paint.getTextBounds(words[0], 0, words[0].length(), rect);
			drawText(canvas, words[0], x - rect.width()/2, y - offset, paint, charWidth);
			for(int i = 1; i < words.length; i++){
				paint.getTextBounds(words[i-1], 0, words[i-1].length(), rect);
				drawText(canvas, words[i],  x - rect.width()/2, y  + i*rect.height() +  offset, paint, charWidth);
			}
		}//end else
		//position of text
	}
	
	@Override
	public boolean isPointOf(float x, float y) {
		// TODO Auto-generated method stub
		double d = Math.sqrt((x-cx)*(x-cx)+(y-cy)*(y-cy));
		
		if (d<radius)
			return true;
		else
			return false;
	}
	
	
	@Override
	public void onUpdate(double t) {
		// TODO Auto-generated method stub
	}//end onUpdate
	@Override
	public void changeColor(int color) {
		// TODO Auto-generated method stub
	}

}//end CircleModel
